package de.gameduell.framework.command {

	/**
	 * @author cbra
	 */
	public class History {
		
		private static var INSTANCE:History = null;
		private static var SEPARATOR:String = ";";
		
		private var _commands:Array;
		
		public static function getInstance():History{
			if(INSTANCE == null){
				INSTANCE = new History();		
			}
			return INSTANCE;
		}
		
		public function History(){
			_commands = new Array();
		}
		
		/**
		 * returns all entries
		 * 
		 * @return the entries of the history
		 */
		public function get commands():Array{
			return _commands;
		}
		
		/**
		 * adds a command to the history
		 * 
		 * @param command the command wich will be added
		 */
		public function addCommand(command:ICommand):void{
			_commands.push(command);
		}
		
		/**
		 * removes all commands from the history
		 * 
		 */
		public function clear():void {
			_commands = new Array();
		}

		/**
		 * returns the last entry
		 * 
		 * @return the last entry
		 */
		public function getLastCommand():ICommand{
			return _commands[_commands.length-1];
		}
		
		/**
		 * return the history converted into a string
		 */
		public function toString():String{
//			Debug.trace("History::toString _commands.length="+_commands.length);
			var result:String = "";
			for(var i:int = 0; i < _commands.length;i++){
//				Debug.trace("History::toString loop");				
//				Debug.trace("History::toString _commands.toString="+_commands[i].toString());				
				result+=_commands[i].toString();
				if(i!=_commands.length-1){
					result+=SEPARATOR;
				}
			}
//			Debug.trace("History::toString result="+result);
			return result;
		}
	}
}



